﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.MvcPager;
using MvcMall.Core.Enums;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.StoreApplies
{
    public partial class Index : ManageBasePage
    {
        private IStoreApplyTask _storeApplyTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("ShenHeStoreApply");
            _storeApplyTask = GetInstance<IStoreApplyTask>();

            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            var list = _storeApplyTask.GetDSList(ANPager.CurrentPageIndex, ANPager.PageSize);
            var dtoList = list.Select(c => new StoreApplyDto
                                               {
                                                   Id = c.Id,
                                                   CreateTime = c.CreateTime,
                                                   IdentityCardId = c.IdentityCardId,
                                                   IdentityCardImage = c.IdentityCardImage,
                                                   LicenseImage = c.LicenseImage,
                                                   OwnerName = c.OwnerName,
                                                   PromoteUserName =
                                                       c.PromoteUser == null ? string.Empty : c.PromoteUser.UserName,
                                                   StoreApplyStatus = c.StoreApplyStatus,
                                                   StoreCatName = c.StoreCat.CatName,
                                                   StoreGradeName = c.StoreGrade.GradeName,
                                                   StoreName = c.StoreName,
                                                   StoreType = c.StoreType,
                                                   UserName = c.User.UserName
                                               }).ToPagedList(ANPager.CurrentPageIndex, ANPager.PageSize,
                                                              list.TotalItemCount);
            rptRoles.DataSource = dtoList;
            rptRoles.DataBind();
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }


        protected void rptRoles_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.CommandName == "Audit")
                {
                    // 审核通过
                    var id = e.Item.FindControl("ltlId") as Literal;
                    var model = _storeApplyTask.Get(int.Parse(id.Text));
                    if (model.StoreApplyStatus == StoreApplyStatus.待审核)
                    {
                        using (var work = UnitOfWork.Begin())
                        {
                            _storeApplyTask.ShenHe(model.Id);
                            work.Commit();
                        }
                        BindRpt();

                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');", true);
                        return;
                    }

                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('只允许待审核申请设置为审核通过！');", true);
                    return;



                }
                else if (e.CommandName == "NoAudit")
                {
                    // 审核不通过
                    var id = e.Item.FindControl("ltlId") as Literal;
                    var model = _storeApplyTask.Get(int.Parse(id.Text));
                    if (model.StoreApplyStatus == StoreApplyStatus.待审核)
                    {
                        using (var work = UnitOfWork.Begin())
                        {
                            _storeApplyTask.Deny(model.Id);
                            work.Commit();
                        }
                        BindRpt();
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');", true);
                        return;
                    }

                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('只允许待审核申请设置为审核不通过！');", true);
                    return;

                }
            }
        }
    }
}